package com.hkts.naturalgas.utils;

import org.apache.commons.lang.StringUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassNameDataHandlerUtils
 * @Description
 * @Author yck
 * @Date2021-05-31 10:33
 * @Version V1.0
 **/
public class DataHandlerUtils {
    /**
     * 功能描述:
     * 数据增长10%  再取加  0.05到-0.05随机数
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-05-31 10:34
     * @Description //TODO
     */
    public static float dataHandler(float num) {
        if (num == 0) {
            return num;
        }
        num = num * ((float) 1.015);
        int max = 200, min = -200;
        int ran2 = (int) (Math.random() * (max - min) + min);
        float a = (float) ran2 / 10000;
        num = num + a;
        if (num < 0) {
            return num - a;
        }
        return num;
    }

    /**
     * 功能描述:
     * 日期处理 按当前日期和 整数处理时间
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-05-31 10:53
     * @Description //TODO
     */
    public static LocalDateTime dateHandler(LocalDateTime time, int i) {
        Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.add(Calendar.SECOND, 60 * i);
        date = c.getTime();
        LocalDateTime ldt = date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return ldt;
    }

    /**
     * 功能描述:
     * [date, i]
     * date 当前日期 i需要添加的秒数
     *
     * @return: java.util.Date
     * @Author: 杨春科
     * @Date: 2021-05-31 16:50
     * @Description //TODO
     */
    public static Date dateAddHandler(Date date, int i) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.add(Calendar.MINUTE, i);
        date = c.getTime();
        return date;
    }

    /* 功能描述:
     *把当前时间解析再加30分钟
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-04 9:43
     * @Description //TODO
     */
    public static LocalDateTime dateAddHandler11(Date date, int i) throws ParseException {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.add(Calendar.MINUTE, 30 + (2 * i) * 30);
        date = c.getTime();
        LocalDateTime ldt = date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return ldt;
    }

    /**
     * 功能描述:
     * 获取当前时间后一个小时内地时间列表
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-08 9:51
     * @Description //TODO
     */
    public static List<LocalDateTime> getAllHourse(Date date) throws ParseException {
        List<LocalDateTime> list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            Calendar c = new GregorianCalendar();
            c.setTime(date);
            c.add(Calendar.MINUTE, i);
            Date time = c.getTime();
            LocalDateTime ldt = time.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            list.add(ldt);
        }
        return list;
    }


    /**
     * 功能描述:
     * 获取指定区间的浮点型随机数
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-04 9:49
     * @Description //TODO
     */
    public static List<Float> getRundom(float min, float max, String bs) {
        List<Float> list = new ArrayList<Float>();
        //没有规律
        if (StringUtils.isBlank(bs)) {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                // list.add(new BigDecimal(ran2/100).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
                list.add((float) ran2 / 10);
            }
            //升序
        } else if (bs.equals("1")) {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                //list.add(new BigDecimal(ran2/100).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());

                list.add((float) ran2 / 10);
            }
            //整体排序
            List<Float> collect = list.stream().sorted().collect(Collectors.toList());
            List<Float> list1 = new ArrayList<Float>();
            List<Float> list2 = new ArrayList<Float>();
            list1 = collect.subList(0, 6);
            list2 = collect.subList(6, 12);
            Collections.shuffle(list1);
            Collections.shuffle(list2);
            list.clear();
            list.addAll(list1);
            list.addAll(list2);
            //降序
        } else {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                //list.add(new BigDecimal(ran2/100).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
                list.add((float) ran2 / 10);
            }
            //整体排序
            List<Float> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            List<Float> list1 = new ArrayList<Float>();
            List<Float> list2 = new ArrayList<Float>();
            list1 = collect.subList(0, 6);
            list2 = collect.subList(6, 12);
            Collections.shuffle(list1);
            Collections.shuffle(list2);
            list.clear();
            list.addAll(list1);
            list.addAll(list2);
        }
        return list;

    }

    /**
     * 功能描述:
     * 获取指定区间的浮点型随机数版本二
     * *[]  下降大数* 系数
     * * 正序 小数乘以 大系数
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-04 9:49
     * @Description //TODO
     */
    public static List<Float> getRundom2(float min, float max, String bs) {
        List<Float> list = new ArrayList<Float>();
        //没有规律
        if (StringUtils.isBlank(bs)) {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                // list.add(new BigDecimal(ran2/100).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
                list.add((float) ran2 / 10);
            }
            //升序
        } else if (bs.equals("1")) {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                //list.add(new BigDecimal(ran2/100).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());

                list.add((float) ran2 / 10);
            }
            //整体排序
            List<Float> collect = list.stream().sorted().collect(Collectors.toList());
            List<Float> list1 = new ArrayList<Float>();
            List<Float> list2 = new ArrayList<Float>();
            List<Float> list3 = new ArrayList<Float>();
            list1 = collect.subList(0, 4);
            list2 = collect.subList(4, 8);
            list3 = collect.subList(8, 12);
            Collections.shuffle(list1);
            Collections.shuffle(list2);
            Collections.shuffle(list3);
            list.clear();
            list.addAll(list1);
            list.addAll(list2);
            list.addAll(list3);
            //降序
        } else {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                //list.add(new BigDecimal(ran2/100).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
                list.add((float) ran2 / 10);
            }
            //整体排序
            List<Float> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            List<Float> list1 = new ArrayList<Float>();
            List<Float> list2 = new ArrayList<Float>();
            List<Float> list3 = new ArrayList<Float>();
            list1 = collect.subList(0, 4);
            list2 = collect.subList(4, 8);
            list3 = collect.subList(8, 12);
            Collections.shuffle(list1);
            Collections.shuffle(list2);
            Collections.shuffle(list3);
            list.clear();
            list.addAll(list1);
            list.addAll(list2);
            list.addAll(list3);
        }
        return list;

    }

    /**
     * 功能描述:
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-04 18:35
     * @Description //TODO
     */
    public static List<Float> getRundom4(float min, float max) {

        List<Float> list = new ArrayList<Float>();
        //没有规律

            for (int i = 0; i < 60; i++) {
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                list.add((float) ran2 / 10);
            }
            //整体排序
            List<Float> collect = list.stream().sorted().collect(Collectors.toList());
            List<Float> list1 = new ArrayList<Float>();
            List<Float> list2 = new ArrayList<Float>();
            List<Float> list3 = new ArrayList<Float>();
            List<Float> list4 = new ArrayList<Float>();
            list1 = collect.subList(0,10);
            list2=collect.subList(10,30);
            list3=collect.subList(30,45);
            list4=collect.subList(45,60);
            Collections.shuffle(list1);
            Collections.shuffle(list2);
            Collections.shuffle(list3);
            Collections.shuffle(list4);
            list.clear();
            list.addAll(list1);
            list.addAll(list2);
            list.addAll(list3);
            list.addAll(list4);
            list.set(0,min);
            list.set(list.size()-1,max);
            //降序

        return list;
    }

    /**
     * 功能描述:
     * 获取指定区间的浮点型随机数
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-04 9:49
     * @Description //TODO
     */
    public static List<Float> getRundomInt(float max, float min, String bs) {
        List<Float> list = new ArrayList<Float>();
        //没有规律
        if (StringUtils.isBlank(bs)) {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max - min) + min);
                list.add((float) ran2);
            }

        } else if (bs.equals("1")) {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max - min) + min);
                list.add((float) ran2);
            }
            //整体排序
            List<Float> collect = list.stream().sorted().collect(Collectors.toList());
            List<Float> list1 = new ArrayList<Float>();
            List<Float> list2 = new ArrayList<Float>();
            list1 = collect.subList(0, 6);
            list2 = collect.subList(6, 12);
            Collections.shuffle(list1);
            Collections.shuffle(list2);
            list.clear();
            list.addAll(list1);
            list.addAll(list2);

        } else {
            for (int i = 0; i < 12; i++) {
                int ran2 = (int) (Math.random() * (max - min) + min);
                list.add((float) ran2 / 100);
            }
            //整体排序
            List<Float> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            List<Float> list1 = new ArrayList<Float>();
            List<Float> list2 = new ArrayList<Float>();
            list1 = collect.subList(0, 6);
            list2 = collect.subList(6, 12);
            Collections.shuffle(list1);
            Collections.shuffle(list2);
            list.clear();
            list.addAll(list1);
            list.addAll(list2);
        }
        return list;

    }

    /**
     * 功能描述:
     * 将数值增大或减小0.
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-08 17:28
     * @Description //TODO
     */
    public static Float getRundomFloat(float value) throws ParseException {
        float ran2 =5- (float) Math.random() * (10);
        value=value+(float)ran2/100;
        DecimalFormat df = new DecimalFormat("#.00");
        String format = df.format(value);
        return Float.parseFloat(format);
    }
    /** 功能描述:
    *处理电流
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-15 14:18
    * @Description //TODO
    */
    public static Float electricDataHandler(float value) throws ParseException {
        if(value==0){
            return value;
        }
        float ran2 =(3- (float) Math.random() * (6))/100;
        DecimalFormat df = new DecimalFormat("#.00");
        String format = df.format(value+ran2);
        return Float.parseFloat(format);
    }
    /** 功能描述:
    * 字符串--- 处理
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-16 16:06
    * @Description //TODO
    */
    public static Float symbolDataHandler(String value) throws ParseException {
        if(value.trim().equals("---")){
            return 0f;
        }
        return Float.parseFloat(value);
    }
    /** 功能描述:
    *根据开始时间返回float 集合
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-17 10:56
    * @Description //TODO
    */
    public static List<LocalDateTime> getAllMinites(String beginDate,int value) throws ParseException {
        List<LocalDateTime> list = new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = format.parse(beginDate);
        for (int i = 0; i < value*60; i++) {
            Calendar c = new GregorianCalendar();
            c.setTime(parse);
            c.add(Calendar.MINUTE, i);
            Date time = c.getTime();
            LocalDateTime ldt = time.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            list.add(ldt);
        }
        return list;
    }

    public static List<LocalDateTime> getAllMinites(String beginDate,String endDate) throws ParseException {
        List<LocalDateTime> list = new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = format.parse(beginDate);
        Date parse2 =format.parse(endDate);
        long between = (parse2.getTime() - parse .getTime())/1000;

        long min = between/60;

        for (int i = 0; i < min; i++) {
            Calendar c = new GregorianCalendar();
            c.setTime(parse);
            c.add(Calendar.MINUTE, i);
            Date time = c.getTime();
            LocalDateTime ldt = time.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            list.add(ldt);
        }
        return list;
    }
    /** 功能描述:
    *
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-18 10:59
    * @Description //TODO
    */
    public static List<Float> getHandlerData(float max,float min){
        List<Float> list = new ArrayList<Float>();
        if(max==0){
            for(int i=0;i<60;i++){
                list.add(0f);
            }
        }else if(max!=0&&min==0){
            for(int i=0;i<60;i++){
                float ran2 =(3- (float) Math.random() * (6))/10;
                list.add(max+ran2);
            }
        }else if(max!=0&&min!=0){
            for(int i=0;i<60;i++){
                int ran2 = (int) (Math.random() * (max * 100 - min * 100) + min * 100) / 10;
                list.add((float) ran2 / 10);
            }
        }else {
            for(int i=0;i<60;i++){
                list.add(0f);
            }
        }

       return  list;
    }
    /**
     * 功能描述:
     * 将数值增大或减小0.
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-08 17:28
     * @Description //TODO
     */
    public static Float getRundomFloatOne(float value) throws ParseException {
        float ran2 =5- (float) Math.random() * (10);
        value=value+(float)ran2/100;
        DecimalFormat df = new DecimalFormat("#.0");
        String format = df.format(value);
        return Float.parseFloat(format);
    }
    /** 功能描述:
    *
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-20 17:57
    * @Description //TODO 根据当前值进行处理加 3到-3
    */
    public static Float getRundomInt(float value) {
        if(value<10){
            return value;
        }
       int z= (int)(0+Math.random()*6);
       int i=3-z;
        if(value+i<0){
            return 0f;
        }
        return value+i;
    }
    /** 功能描述:
    *
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-23 9:31
    * @Description //TODO 处理--- 将---转化为null
    */
    public static  Float handlerStr(String str){
        if(str.trim().equals("---")){
            return null;
        }
        return Float.parseFloat(str);
    }
    /** 功能描述:
    *处理曲线随机数
    * @return: 
    * @Author: 杨春科
    * @Date: 2021-06-26 15:47
    * @Description //TODO 
    */
    public static  Float getData(Float value){
        if(value<10){
            return value;
        }
        int z=0;
        int i=0;
        z= (int)(0+Math.random()*4);
        i=2-z;
        value=value+i;
        int i1 = value.intValue();
        return (float)i1;
    }
    /** 功能描述:
    *
    * @return: 
    * @Author: 杨春科
    * @Date: 2021-06-28 9:42
    * @Description //TODO 
    */
    public static int getRundom(){
       int z= (int)(0+Math.random()*10);
       if(z%2==0){
           return 1;
       }
       return 2;
    }
    /** 功能描述:
    *  曲线压力 随机数处理
     *  90%  数据在 65-85  10% 在90-100
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-29 17:48
    * @Description //TODO
    */
    public static Float getPressureRandomNumber(){
        int ran2 = (int) (Math.random()*(10-1)+1);
        if(ran2%7==0){
           //生成90-100随机数
            int ran3 = (int) (Math.random()*(100-90)+90);
            return (float)ran3;

        }else{
            int ran3 = (int) (Math.random()*(85-65)+65);
            return (float)ran3;
        }
    }
    /** 功能描述:
    *  bs==1 升序
     *  bs==0 降序
    * @return: 
    * @Author: 杨春科
    * @Date: 2021-06-30 15:31
    * @Description //TODO 
    */
   public static List<Float> getTemperatureCurve(float mbt,float currentt,int length,String order){
       List<Float> list=new ArrayList<>();
       for(int i=0;i<length;i++){
           list.add(mbt+i*(currentt-mbt)/length);
       }
     if(order.equals("desc")){
       return   list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
     }
     return list;
   }
}